﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuanLyQuangCao
{
    public partial class frmNHANVIEN_Edit : Form
    {
        public string strconn = @"Data Source=thonglh;Initial Catalog=QuanLyQuangCao;Integrated Security=True";
        SqlConnection sc = new SqlConnection();
        SqlCommand scmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        BindingSource bds = new BindingSource();

        private string maNhanVien;

        public void setMaNhanVien(string maNhanVien)
        {
            this.maNhanVien = maNhanVien;
        }

        public enum myEdit
        {
            New = 1,
            Edit = 2,
        }

        public myEdit myNew_Edit;

        public void Load(myEdit myNew_Edit)
        {
            this.myNew_Edit = myNew_Edit;
        }

        public frmNHANVIEN_Edit()
        {
            InitializeComponent();

            this.btDongY.Click += new EventHandler(btDongY_Click);
            this.btHuy.Click += new EventHandler(btHuy_Click);
        }

        public void btDongY_Click(object sender, EventArgs e)
        {
            if (myNew_Edit == myEdit.New)
            {
                string strMa_Nhan_Vien = txtMa_Nhan_Vien.Text;
                string strTen_Nhan_Vien = txtTen_Nhan_Vien.Text;
                string strMat_Khau = txtMat_Khau.Text;
                int bTrang_Thai = Convert.ToInt16(txtTrang_Thai.Text);
                string strKieu = txtKieu.Text;

                string sql =
                    "INSERT NHANVIEN (Ma_Nhan_Vien, Ten_Nhan_Vien, MaT_Khau, Trang_Thai, Kieu)" +
                    "VALUES ('" + strMa_Nhan_Vien + "' , '" + strTen_Nhan_Vien + "', '" + strMat_Khau + "', '" + bTrang_Thai + "', '" + strKieu + "')";
                sc = new SqlConnection(strconn);

                scmd = new SqlCommand(sql, sc);

                sc.Open();
                scmd.ExecuteNonQuery();

                CapNhat();

                sc.Close();
                this.Close();
            }

            if (myNew_Edit == myEdit.Edit)
            {
                string strMa_Nhan_Vien = txtMa_Nhan_Vien.Text;
                sc = new SqlConnection(strconn);
                string str =    "UPDATE NHANVIEN SET Ma_Nhan_Vien = '" + txtMa_Nhan_Vien.Text +"', Ten_Nhan_Vien = '" + txtTen_Nhan_Vien.Text + "', " + 
                                "MaT_Khau = '" + txtMat_Khau.Text +"', Trang_Thai = '" + txtTrang_Thai.Text + "', Kieu = '"  + txtKieu + "'" +
                                "WHERE Ma_Nhan_Vien = '" + maNhanVien + "'";

                scmd = new SqlCommand(str, sc);
                sc.Open();
                scmd.ExecuteNonQuery();
                CapNhat();
                sc.Close();
                this.Close();
            }
            
            
        }

        public void btHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void CapNhat()
        {
            //Cap nhat lai du lieu cho frmDangKy_View
            string strKhachHang = @"SELECT * FROM NhanVien";
            frmNhanVien_View.view.myconn.GetData(strKhachHang, strconn, ds, "NhanVien");
            da = new SqlDataAdapter(strKhachHang, strconn);
            dt = new DataTable();
            da.Fill(dt);
            bds.DataSource = dt;
            frmNhanVien_View.view.bds.DataSource = dt;
            frmNhanVien_View.view.Update(frmNhanVien_View.view.bds);
            
        }

    }
}
